/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source 
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.  
 */

package jdbcmanager.tree;

import java.awt.*;

import jdbcmanager.swing.*;
import jdbcmanager.app.*;
import jdbcmanager.dbtools.*;
import jdbcmanager.dbtools.datatypes.*;

/**
 * Tree nodes representing an "Store Procedure" DB entity.
 *
 * @author Francisco Morero Peyrona
 */
final class NodeProcedure extends JdbcTreeNode
{
   private JFrameMain.JDBCManagerInternalFrame frame = null;
   private boolean bSPSupported = false;   // Does the DBMS support Stored Procedures?

   /**
    * Called to create the parent of all Stored Procedure nodes.
    * 
    * @param sCatalog The catalog to which all SP belong to
    */
   NodeProcedure( String sCatalog, String sSchema )
   {
      super( icon_tree_procedure, 
             new DbEntityDescriptor( sCatalog, sSchema, DbUtil.getAgentDBMS().getAgentMetaData().getProcedureTerm() +'s' ) );
      
      this.bSPSupported = DbUtil.getAgentDBMS().getAgentMetaData().supportsStoredProcedures();
      
      if( this.bSPSupported )
         add( new NodeCatalog( "" ) ); // Trick to force the tree to show the RootHandles 
                                       // (it will be removed before adding real nodes)
      setInvalidActions();
   }
   
   /**
    * Called once per each SP node available in designated catalog. 
    * 
    * @param ded
    * @param bProcedure
    */
   NodeProcedure( DbEntityDescriptor ded, boolean bProcedure )
   {
      super( icon_tree_procedure, ded );

      setAllowsChildren( ! bProcedure );
      setInvalidActions();
   }

   boolean isInitialised()
   {
      return this.bWasInit || ! getAllowsChildren();
   }

   // NEXT: implementar: new(), edit(), rename() y delete()
   boolean new_()
   {
      App.showMessage( "Option under development." );

      return false;
   }

   void open()
   {
      if( this.frame == null || this.frame.isClosed() )
      {
         App.setCursor( Cursor.WAIT_CURSOR );
         this.frame = App.getFrameMain().openProcedure( ded );
         App.restoreCursor();
      }
      else
      {
         this.frame.setSelected( true );
         this.frame.restore();
      }
   }

   boolean edit()
   {
      App.showMessage( "Option under development." );

      return false;
   }
   
   boolean rename( String sNewName )
   {
      App.showMessage( "Rename", "Option under development." );
      /*
      if( this.frame != null )
         this.frame.rename( sNewName );
      */
      return false;  
   }   

   boolean delete()
   {
      App.showMessage( "Option under development." );
      /*
      if( this.frame != null )
         this.frame.dispose();
       */
      return false;
   }

   void info()
   {
      // Nothing to do
   }

   private void setInvalidActions()
   {
      if( getAllowsChildren() )
      {
      	 setNewable(     this.bSPSupported );
         setOpenable(    false );
         setEditable(    false );
         setDeleteable(  false );
         setRefreshable( this.bSPSupported );
      }
      else
      {
         setRefreshable( false );
      }
      
      setInfoable( false );
   }
}